//
//  tradersOnMapViewController.m
//  tradersOnMap
//
//  Created by bilmuh bilmuh on 24.02.2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TradersOnMapViewController.h"
#import "ProfileViewController.h"
#import "SoldItemsViewController.h"
#import "LoginBrain.h"
#import "MapViewController.h"

@implementation tradersOnMapViewController

@synthesize brain, sbrain, signUpView;

-(LoginBrain *)brain
{
	if (!brain) {
		brain = [[LoginBrain alloc] init];
	}
	return brain;
}

//For close the keyboard
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}

-(IBAction)loginButtonPressed:(UIButton *)sender
{
	[[self brain] login];
	if (![[self brain].userId isEqual:@"NULL"]) {
		[tradersOnMapViewController createTabBarController];
		[self.signUpView release];
		[self release];
	}
	else {
		UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Unknown User" message:@"Please Check Your E-mail or Password" 
													delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
		[av show];
		[av release];
	}
}

-(IBAction)signupButtonPressed:(UIButton *)sender
{	
	if (signUpView.hidden) {
		[signUpView setHidden:FALSE];
	}
	else {
		[self.view addSubview:signUpView];
		[self.view bringSubviewToFront:signUpView];
	}
}

+(void)createTabBarController;
{	
	UITabBarController *tbc = [[UITabBarController alloc] init];
	
	ProfileViewController *vc1 = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
    UINavigationController* vc2 = [[UINavigationController alloc] initWithRootViewController:[[SoldItemsViewController alloc] init]];
	MapViewController *vc3 = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
	
	[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, nil]];

	[[[UIApplication sharedApplication] keyWindow] addSubview:tbc.view];

	[vc1 release], [vc2 release], [vc3 release];
}

-(IBAction)setEmail:(UITextField *)textField
{
	[self brain].email = [textField text];
}

-(IBAction)setPassword:(UITextField *)textField
{
	[self brain].password = [textField text];
}

-(SignUpBrain *)sbrain
{
	if (!sbrain) {
		sbrain = [[SignUpBrain alloc] init];
	}
	return sbrain;
}

-(IBAction)backButtonPressed
{
	[signUpView setHidden:YES];
}

-(IBAction)signUpButton2Pressed:(UIButton *)button
{	
	[[self sbrain] signUp];
	if ([[self sbrain].error isEqualToString:@""]) {
		[tradersOnMapViewController createTabBarController];
	}
	else {
		UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Duplicate User" message:@"Your e-mail address is already exist" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
		[av show], [av release];
	}
}

-(IBAction)setPassword2:(UITextField *)textField
{
	[self sbrain].password2 = textField.text;
}

-(IBAction)setFirstName:(UITextField *)textField
{
	[self sbrain].firstName = textField.text;
}

-(IBAction)setLastName:(UITextField *)textField
{
	[self sbrain].lastName = textField.text;
}

-(IBAction)sSetEmail:(UITextField *)textField
{
	[self sbrain].email = [textField text];
}

-(IBAction)sSetPassword:(UITextField *)textField
{
	[self sbrain].password = [textField text];
}

@end
